var WorkBiz = {
		initialize : function(){

			// Log-In Check
			$('#btn_refresh').on("click" ,function(){			
				COMMAJAX.common_ajax({
					url : "/mind/join/selectListLogMaster.do"
						,callback_fnc :  function(data){

							$('table tr:gt(0)').empty();
							$('table:eq(1)').remove();

							var tag = '';

							if($('#btncheckingLog').hasClass('buttonactive')){
								$.each(data.rows, function(){
									tag += '<tr style="text-align:center" >';
									tag += '<td>' + this.USER_NAME + '</td>';
									tag += '<td>' + this.USER_LOG_CNT + '</td>';
									tag += '<td>' + this.USER_REFRESH_CNT + '</td>';
									tag += '<td>' + this.USER_NEWSCREEN_CNT + '</td>';
									tag += '<td>' + this.TOT_STAY_TIME + '</td>';
									tag += '</tr>';	
								});
							}

							if($('#btncheckingTime').hasClass('buttonactive')){
								$.each(data.rows, function(){
									tag += '<tr style="text-align:center" >';
									tag += '<td>' + this.USER_NAME + '</td>';
									tag += '</tr>';	
								});
							}

							$('table:eq(0) tr:last').after(tag);
						}
				});
			});

			$('#btncheckingLog').on("click" ,function(){			
				COMMAJAX.common_ajax({
					url : "/mind/join/selectListLogMaster.do"
						,callback_fnc :  function(data){
							
							$('#btn_refresh').css("visibility", "visible");

							$('table:eq(0)').empty();
							$('table:eq(1)').remove();
							$('table').css("width", "800px")

							var tag = '';

							tag += '<tr>';
							tag += '<th>사용자명  </th>';
							tag += '<th>로그인 횟수 </th>';
							tag += '<th>새로고침 클릭 횟수  </th>';
							tag += '<th>새화면 클릭 횟수  </th>';
							tag += '<th>자기글 페이지에 머문 시간 (총시간)  </th>';
							tag += '</tr>';

							$.each(data.rows, function(){
								tag += '<tr style="text-align:center" >';
								tag += '<td>' + this.USER_NAME + '</td>';
								tag += '<td>' + this.USER_LOG_CNT + '</td>';
								tag += '<td>' + this.USER_REFRESH_CNT + '</td>';
								tag += '<td>' + this.USER_NEWSCREEN_CNT + '</td>';
								tag += '<td>' + this.TOT_STAY_TIME + '</td>';
								tag += '</tr>';	
							});

							$('table:eq(0)').append(tag);

						}
				});
			});

			$('#btncheckingTime').on("click" ,function(){			
				COMMAJAX.common_ajax({
					url : "/mind/join/selectListLogMaster.do"
					,callback_fnc :  function(data){
						
						$('#btn_refresh').css("visibility", "hidden");

							$('table').empty();
							$('#detailTable').remove();
							$('table').empty();
							$('table').css("left", "0px");
							$('table').css("width", "100px");
							$('table').css("float", "left");

							var tag = '';
							tag += '<tr>';
							tag += '<td colspan="2" style="height:20px"  ></td>';
							tag += '</tr>';
							tag += '<tr>';
							tag += '<td style="text-align:center" >사용자명</td>';
							tag += '</tr>';

							$.each(data.rows, function(index, value){
								tag += '<tr style="text-align:center" >';
								tag += '<td><a id="detail' + index  + '" >' + this.USER_NAME + '</a></td>';
								tag += '</tr>';	


							});

							var table = '<table id="detailTable"  border="1"   style="width: 600px">';
							table += '<tr style="text-align:center" ><th></th><th >자신의 작성글 페이지</th><th></th></tr>';
							table += '<tr>';
							table += '<th>사용 시작 시간  </th>';
							table += '<th>사용 종료 시간  </th>';
							table += '<th>머문 시간 (/초) </th>';
							table += '</tr>';

							$('table').append(tag);
							$('table').after(table);



							$.each(data.rows, function(index, value){
								$('#detail' + index).on("click" ,function(){	
									var name = $(this).text();

									$(':not(#detail' + index + ')').css('color', '');

									$('#detail' + index).css('color',  'red');

									COMMAJAX.common_ajax({
										url : "/mind/join/selectListStayTimeByUser.do"
											,data : {USER_NAME  : name}
									,callback_fnc :  function(data){
										$('#detailTable tbody tr:gt(1)').empty();

										var tag = '';

										var sum = 0;
										var totalSum = '';
										$.each(data.rows, function(index, value){
											sum += Number(this.STAY_TIME);
										});

										totalSum = (sum/60).toFixed(4); 
										
										var secondTotal = (totalSum).split('.');
										

										$.each(data.rows, function(){
											tag += '<tr style="text-align:center" >';
											tag += '<td>' + this.USER_STAY_START_DATE + '</td>';
											tag += '<td>' + this.USER_STAY_END_DATE + '</td>';
											tag += '<td>' + this.STAY_TIME + '</td>';
											tag += '</tr>';	
										});

										if(data.rows.length > 0){
											tag += '<tr style="text-align:center" >';
											tag += '<td></td>';
											tag += '<td></td>';
											tag += '<td>' + secondTotal[0] + ":" + secondTotal[1].slice(0, 2) + ":" + secondTotal[1].slice(2,4)  + '</td>';
											tag += '</tr>';	
										}

										$('#detailTable tbody tr:last').after(tag);

									}
									});
								});
							});
						}
				});
			});
			
			
			$('#btncheckingResTime').on("click" ,function(){			
				COMMAJAX.common_ajax({
					url : "/mind/join/selectListLogMaster.do"
					,callback_fnc :  function(data){

						$('#btn_refresh').css("visibility", "hidden");
						
							$('table').empty();
							$('#detailTable').remove();
							$('table').empty();
							$('table').css("left", "0px");
							$('table').css("width", "100px");
							$('table').css("float", "left");

							var tag = '';
							tag += '<tr>';
							tag += '<td colspan="2" style="height:20px"  ></td>';
							tag += '</tr>';
							tag += '<tr>';
							tag += '<td style="text-align:center" >사용자명</td>';
							tag += '</tr>';

							$.each(data.rows, function(index, value){
								tag += '<tr style="text-align:center" >';
								tag += '<td><a id="detail' + index  + '" >' + this.USER_NAME + '</a></td>';
								tag += '</tr>';	


							});

							var table = '<table id="detailTable" border="1"  style="width: 600px">';
							table += '<tr style="text-align:center" ><th></th><th  >알람 피드백</th><th></th><th></th></tr>';
							table += '<tr>';
							table += '<th>알람 형태 </th>';
							table += '<th>알람 시간 </th>';
							table += '<th>반응 시간 </th>';
							table += '<th>걸린 시간 (/초) </th>';
							table += '</tr>';

							$('table').append(tag);
							$('table').after(table);



							$.each(data.rows, function(index, value){
								$('#detail' + index).on("click" ,function(){	
									var name = $(this).text();

									$(':not(#detail' + index + ')').css('color', '');

									$('#detail' + index).css('color',  'red');

									COMMAJAX.common_ajax({
										url : "/mind/join/selectListReplyTimeByUser.do"
											,data : {USER_NAME  : name}
									,callback_fnc :  function(data){
										$('#detailTable tbody tr:gt(1)').empty();

										var tag = '';

										var sum = 0;
										var totalSum = '';
										$.each(data.rows, function(index, value){
											sum += Number(this.PERIOD_TIME);
										});

                                        totalSum = (sum/60).toFixed(4);

                                        var secondTotal = (totalSum).split('.');

										$.each(data.rows, function(){
											tag += '<tr style="text-align:center" >';
											tag += '<td>' + '알람' + '</td>';
											tag += '<td>' + this.USER_START_DATE + '</td>';
											tag += '<td>' + this.USER_END_DATE + '</td>';
											tag += '<td>' + this.PERIOD_TIME + '</td>';
											tag += '</tr>';	
										});

										if(data.rows.length > 0){
											tag += '<tr style="text-align:center" >';
											tag += '<td></td>';
											tag += '<td></td>';
											tag += '<td></td>';
											tag += '<td>'  + secondTotal[0] + ":" + secondTotal[1].slice(0, 2) + ":" + secondTotal[1].slice(2,4)  + '</td>';
											tag += '</tr>';	
										}

										$('#detailTable tbody tr:last').after(tag);

									}
									});
								});
							});
						}
				});
			});

		}
}


!function($){
	WorkBiz.initialize();	
}(jQuery);